package com.bozhong.config.parser;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author fupan
 */
public class NacosDataPropertiesParser extends AbstractNacosDataParser {
	private static final Logger log = LoggerFactory.getLogger(NacosDataPropertiesParser.class);

	public NacosDataPropertiesParser() {
		super("properties");
	}

	@Override
	protected Map<String, Object> doParse(String data) throws IOException {
		Map<String, Object> result = new LinkedHashMap();
		BufferedReader reader = new BufferedReader(new StringReader(data));
		Throwable var4 = null;

		try {
			for(String line = reader.readLine(); line != null; line = reader.readLine()) {
				String dataLine = line.trim();
				if (!StringUtils.isEmpty(dataLine) && !dataLine.startsWith("#")) {
					int index = dataLine.indexOf("=");
					if (index == -1) {
						log.warn("the config data is invalid {}", dataLine);
					} else {
						String key = dataLine.substring(0, index);
						String value = dataLine.substring(index + 1);
						result.put(key.trim(), value.trim());
					}
				}
			}
		} catch (Throwable var17) {
			var4 = var17;
			throw var17;
		} finally {
			if (reader != null) {
				if (var4 != null) {
					try {
						reader.close();
					} catch (Throwable var16) {
						var4.addSuppressed(var16);
					}
				} else {
					reader.close();
				}
			}

		}

		return result;
	}
}